package com.hontech.bread.store

import com.hontech.bread.utils.OrderPayInfo
import com.hontech.bread.utils.currentTimestamp
import com.hontech.bread.utils.runCPU


class Order(
    val id: Long,
    val createTimestamp: Long,
    val name: String,
    val productId: String,
    val price: Int,
    val cargo: String, // ${stringById(R.string.t1733460848_285)}
    val coin: Int, // ${stringById(R.string.t1733460848_286)}
    val bill: Int, // ${stringById(R.string.t1733460848_287)}
    val pos: Int, // ${stringById(R.string.t1733460848_288)}
    val change: Int, // ${stringById(R.string.t1733460848_289)}
    val tag: Int,
    val remark: String
) {

    companion object {

        private const val TB_NAME = "tb_order"

        const val TAG_OK = 0

        const val REMARK_SUCC = "succ"
        const val REMARK_FAIL = "fail"

        const val CREATE_SQL = "CREATE TABLE IF NOT EXISTS $TB_NAME (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "create_timestamp INTEGER NOT NULL, " +
                "name TEXT NOT NULL, " +
                "product_id TEXT NOT NULL, " +
                "price INTEGER NOT NULL, " +
                "cargo TEXT NOT NULL, " +
                "coin INTEGER NOT NULL, " +
                "bill INTEGER NOT NULL, " +
                "pos INTEGER NOT NULL, " +
                "change INTEGER NOT NULL, " +
                "tag INTEGER NOT NULL, " +
                "remark TEXT NOT NULL" +
                ")"

        const val DROP_SQL = "DROP TABLE IF EXISTS $TB_NAME"

        suspend fun create(
            name: String,
            productId: String,
            price: Int,
            cargo: StoreCargo,
            info: OrderPayInfo,
            tag: Int,
            remark: String
        ) = runCPU {
            Store.db.execSQL(
                "INSERT INTO " +
                        "$TB_NAME (create_timestamp, name, product_id, price, cargo, coin, bill, pos, change, tag, remark) " +
                        "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                arrayOf(
                    currentTimestamp(),
                    name,
                    productId,
                    price,
                    cargo.name(),
                    info.coin,
                    info.bill,
                    info.pos,
                    info.change,
                    tag,
                    remark
                )
            )
        }

        suspend fun query() = runCPU {
            val cursor = Store.db.rawQuery(
                "SELECT " +
                        "id, create_timestamp, name, product_id, price, cargo, coin, bill, pos, change, tag, remark " +
                        "FROM $TB_NAME", arrayOf()
            )
            val list = ArrayList<Order>()
            cursor.use {
                while (cursor.moveToNext()) {
                    val order = Order(
                        cursor.getLong(0),
                        cursor.getLong(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getInt(4),
                        cursor.getString(5),
                        cursor.getInt(6),
                        cursor.getInt(7),
                        cursor.getInt(8),
                        cursor.getInt(9),
                        cursor.getInt(10),
                        cursor.getString(11)
                    )
                    list.add(order)
                }
            }
            list
        }

    }


    suspend fun delete() = runCPU {
        Store.db.execSQL("DELETE FROM $TB_NAME WHERE id = ?", arrayOf(id))
    }
}


